package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.AuditHistoryDao;
import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.AuditHistory;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuditHistoryServiceImpl implements AuditHistoryService {

    @Autowired
    private AuditHistoryDao auditHistoryDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Result getAuditHistory(Integer auditType, Integer relatedId) {
        try {
            if (auditType == null || "".equals(auditType.toString())) {
                return ResultUtil.error(500, "缺少参数auditType", "Miss Param auditType", null);
            }
            if (relatedId == null || "".equals(relatedId.toString())) {
                return ResultUtil.error(500, "缺少参数relatedId", "Miss Param relatedId", null);
            }
            List<AuditHistory> auditHistory = auditHistoryDao.getAuditHistory(auditType, relatedId);
            for (AuditHistory history : auditHistory) {
                history.setAuditUser(userDao.getUserInfoById(history.getAuditUserId()));
                history.setSubmitUser(userDao.getUserInfoById(history.getSubmitUserId()));
            }
            return ResultUtil.success(auditHistory);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
